package com.dcy.daily.common.configuration.mybatis;

import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.List;

/**
 * 加载mybatisLog拦截器
 */
@Configuration
public class MybatisPluginsConfig implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;

    @Value(value = "${snowflake.enabled:false}")
    private boolean autoIdEnabled;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        this.addMyInterceptor();
    }

    private void addMyInterceptor() {
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            if(autoIdEnabled){
                sqlSessionFactory.getConfiguration().addInterceptor(new AutoIdInterceptor());
            }
        }
    }

}
